<?php
namespace app\index\controller;

use app\common\controller\IndexBase;
use app\common\library\YuUtil;
use think\Queue;
use yu\Random;

class My extends IndexBase
{
    public function index()
    {
         echo \think\facade\App::version();
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V5.1<br/><span style="font-size:30px">12载初心不改（2006-2018） - 你值得信赖的PHP框架</span></p></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="eab4b9f840753f8e7"></think>';
    }

    public function hello($name = 'ThinkPHP5')
    {
//        ly(array('da'=>$name,'func'=>'Index-hello'));
        ly('5s6df5','test-'.time());
        return 'hello,' . $name;
    }

    public function sendemailtest(){
        $content = '恭喜您！于'.date('yy').'注册收藏家成功！收藏家地址为：http://www.scj100.com';
//        $re = sendEmailByUid(35,'注册收藏家成功！！',$content);

        echo getpath();

//        if ($re->getStatus()){
//            $this->success('发送邮件成功！');
//        }else{
//            $this->error('发送邮件失败！');
//        }
    }

    /**
     * 一个使用了队列的 action
     */
    public function actionWithHelloJob(){

        // 1.当前任务将由哪个类来负责处理。
        //   当轮到该任务时，系统将生成一个该类的实例，并调用其 fire 方法
        $jobHandlerClassName  = 'app\index\job\Hello';
        // 2.当前任务归属的队列名称，如果为新队列，会自动创建
        $jobQueueName  	  = "helloJobQueue";
        // 3.当前任务所需的业务数据 . 不能为 resource 类型，其他类型最终将转化为json形式的字符串
        //   ( jobData 为对象时，需要在先在此处手动序列化，否则只存储其public属性的键值对)
        $jobData       	  = [ 'ts' => time(), 'bizId' => uniqid() , 'a' => 1 ] ;
        // 4.将该任务推送到消息队列，等待对应的消费者去执行
//        $isPushed = Queue::push( $jobHandlerClassName , $jobData , $jobQueueName );
        // database 驱动时，返回值为 1|false  ;   redis 驱动时，返回值为 随机字符串|false
//        if( $isPushed !== false ){
//            echo date('Y-m-d H:i:s') . " a new Hello Job is Pushed to the MQ"."<br>";
//        }else{
//            echo 'Oops, something went wrong.';
//        }
    }

    public function test(){
    	//打印环境 use think\facade\Env;
    	$this->show(Env::get('root_path'));
    	$this->show(Env::get('think_path'));
    	$this->show(Env::get('config_path'));
    	$this->show(Env::get('extend_path'));
    	$this->show(Env::get('module_path'));
    	$this->show(Env::get('app_path'));

    	$this->show(\think\facade\App::version());
    	///打印配置 use think\facade\Config;
    	var_dump(Config::get('template.tpl_replace_string.__STATIC__'));

    	return;
    }

    public function te(){
    	//获取请求对象
    	// $request = $this->request;
    	//获取请求对象里的服务器路径数组
    	// $da=$request->server();
    	// 如果修改了index.php入口地址，则需要手动修改cdnurl的值
        // $url = preg_replace("/\/(\w+)\.php$/i", '', $da['DOCUMENT_ROOT']);
        
    	// 设置mbstring字符编码
        mb_internal_encoding("UTF-8");

    	// 指定模板输出
		return $this->view->fetch(); 
    }


    public function tesd(){
        $array = array(
            array('id' => 1, 'pid' => 0, 'name' => '河北省'),
            array('id' => 2, 'pid' => 0, 'name' => '北京市'),
            array('id' => 3, 'pid' => 1, 'name' => '邯郸市'),
            array('id' => 4, 'pid' => 2, 'name' => '朝阳区'),
            array('id' => 5, 'pid' => 2, 'name' => '通州区'),
            array('id' => 6, 'pid' => 4, 'name' => '望京'),
            array('id' => 7, 'pid' => 4, 'name' => '酒仙桥'),
            array('id' => 8, 'pid' => 3, 'name' => '永年区'),
            array('id' => 9, 'pid' => 1, 'name' => '武安市'),
        );

        $items = array(
            1 => array('id' => 1, 'pid' => 0, 'name' => '江西省'),
            2 => array('id' => 2, 'pid' => 0, 'name' => '黑龙江省'),
            3 => array('id' => 3, 'pid' => 1, 'name' => '南昌市'),
            4 => array('id' => 4, 'pid' => 2, 'name' => '哈尔滨市'),
            5 => array('id' => 5, 'pid' => 2, 'name' => '鸡西市'),
            6 => array('id' => 6, 'pid' => 4, 'name' => '香坊区'),
            7 => array('id' => 7, 'pid' => 4, 'name' => '南岗区'),
            8 => array('id' => 8, 'pid' => 6, 'name' => '和兴路'),
            9 => array('id' => 9, 'pid' => 7, 'name' => '西大直街'),
            10 => array('id' => 10, 'pid' => 8, 'name' => '东北林业大学'),
            11 => array('id' => 11, 'pid' => 9, 'name' => '哈尔滨工业大学'),
            12 => array('id' => 12, 'pid' => 8, 'name' => '哈尔滨师范大学'),
            13 => array('id' => 13, 'pid' => 1, 'name' => '赣州市'),
            14 => array('id' => 14, 'pid' => 13, 'name' => '赣县'),
            15 => array('id' => 15, 'pid' => 13, 'name' => '于都县'),
            16 => array('id' => 16, 'pid' => 14, 'name' => '茅店镇'),
            17 => array('id' => 17, 'pid' => 14, 'name' => '大田乡'),
            18 => array('id' => 18, 'pid' => 16, 'name' => '义源村'),
            19 => array('id' => 19, 'pid' => 16, 'name' => '上坝村'),
        );

        /*
         * 获得递归完的数据,遍历生成分类
         */
//        $array = $this->getTree($array);
//        foreach ($array as $item) {
//            echo str_repeat('--', $item['level']), $item['name'].'<br />';
//        }
        $array = $this->genTree5($items);
        print_r($array);
//        foreach ($array as $item) {
//            echo str_repeat('--', $item['id']), $item['pid'].'<br />';
//        }


    }


//    public function generateTree($array){
//        //第一步 构造数据
//        $items = array();
//        foreach($array as $value){
//            $items[$value['id']] = $value;
//        }
//        //第二部 遍历数据 生成树状结构
//        $tree = array();
//        foreach($items as $key => $value){
//            if(isset($items[$item['pid']])){
//                $items[$item['pid']]['son'][] = &$items[$key];
//            }else{
//                $tree[] = &$items[$key];
//            }
//        }
//        return $tree;
//    }

    /**
     * 递归实现无限极分类
     * @param $array 分类数据
     * @param $pid 父ID
     * @param $level 分类级别
     * @return $list 分好类的数组 直接遍历即可 $level可以用来遍历缩进
     */
    public function getTree($array, $pid =0, $level = 0){

        //声明静态数组,避免递归调用时,多次声明导致数组覆盖
        static $list = [];
        foreach ($array as $key => $value){
            //第一次遍历,找到父节点为根节点的节点 也就是pid=0的节点
            if ($value['pid'] == $pid){
                //父节点为根节点的节点,级别为0，也就是第一级
                $value['level'] = $level;
                //把数组放到list中
                $list[] = $value;
                //把这个节点从数组中移除,减少后续递归消耗
                unset($array[$key]);
                //开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
                $this->getTree($array, $value['id'], $level+1);
            }
        }
        return $list;
    }

    function genTree5($items) {
        foreach ($items as $item)
            $items[$item['pid']]['son'][$item['id']] = &$items[$item['id']];
        return isset($items[0]['son']) ? $items[0]['son'] : array();
    }

    /**
     * 将数据格式化成树形结构
     * @author Xuefen.Tong
     * @param array $items
     * @return array
     */
    function genTree9($items) {
        $tree = array(); //格式化好的树
        foreach ($items as $item)
            if (isset($items[$item['pid']]))
                $items[$item['pid']]['son'][] = &$items[$item['id']];
            else
                $tree[] = &$items[$item['id']];
        return $tree;
    }

    function sockettest(){
        return $this->view->fetch();
    }

    function t2(){
        $re = \app\common\library\YuPust::getPushClientCount();
        echo '同时在线人数';
        echo $re->getMsg();
        echo '<br>';
        echo $re->getData();
    }

    function t3(){
        $num = Random::uuid();
        $message = $num.'测试消息！！！'.date('y-m-d h:i:s');
        $message = 'location.reload';
        $re = \app\common\library\YuPust::pushMsgToUid(20,$message);
        echo $re->getMsg();
        echo '<br>';
        echo $re->getData();
    }

    function t4(){
//        $num = Random::uuid();
//        $message = $num.'测试消息！！！'.date('y-m-d h:i:s');
        $re = \app\common\library\YuPust::getClientIdByUid(45);
        echo $re->getMsg();
        echo '<br>';
        echo $re->getData();
    }

    function t(){
//        $client_id = '7f00000108ff00000001';
        $num = Random::uuid();
        $message = $num.'测试消息！！！'.date('y-m-d h:i:s');

//        $type = '4465456';
//        $jsonmsg = json_encode(['type'=>$type,'message'=>$message]);

        $client_id ='ac12041208ff00000003';
        $re = \app\common\library\YuPust::pushMsgToClient($client_id,$message);
        echo $re->getMsg();
        echo '<br>';
        echo $re->getData();

//        $url = 'http://test2.yunhaijingcheng.com/t33.php';
//        $key = 'yubenziyou';
//        $pa = 'www.scj100.com';
//        $func = 'sendToClient';
//
//
//        $options=['client_id'=>$client_id,'data'=>$jsonmsg];
//        $postString = ['key'=>$key,'pa'=>$pa,'func'=>$func,'options'=>json_encode($options)];
//        $re = \app\common\library\YuPost::post($url,$postString);
//        echo $re;


//        $ip = '192.168.220.132:1236';
//        $ip = '112.74.208.228:1236';
//        \app\common\library\Gateway::$registerAddress = $ip;
//        \app\common\library\Gateway::sendToClient($client_id,$jsonmsg);
    }


}
